package com.ygqh.baby.shiro.realm;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ygqh.baby.ao.CustomerStatus;
import com.ygqh.baby.model.CustomerUser;
import com.ygqh.baby.model.YgUserModel;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.CustomerUserService;
import com.ygqh.baby.service.YgUserService;

/**
 */
@Component
public class YgUserRealm extends AuthorizingRealm {
	
	@Autowired
    private YgUserService userService;
	@Autowired
	private CustomerUserService customerUserService;

    /**
     * 授权信息
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    	YgUserModel ygUser = (YgUserModel) principals.getPrimaryPrincipal();
    	CustomerUser customerUser = customerUserService.findByUserName(ygUser.getUserName());
		SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
		if (customerUser != null && !customerUser.getCustomerStatus().equals(CustomerStatus.Offline)) {
			authorizationInfo.addRole("customerUser");
		} else {
			authorizationInfo.addRole("ygUser");
		}
        return authorizationInfo;
    }
    
    /**
     * 身份认证
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = (String)token.getPrincipal();
        YgUser user = userService.findByUserName(username);
        if(user == null) {
            throw new UnknownAccountException();//没找到帐号
        }
        YgUserModel userModel = userService.findModelById(user.getId());
        userModel.setIsOnline(Boolean.TRUE);
        String headFileId = userService.saveHeadImageToMongo(user.getHeadImageUrl(), username);
        userModel.setHeadFileId(headFileId);
        //交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配，如果觉得人家的不好可以自定义实现
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
        		userModel, //用户
                user.getPassword(), //密码
                ByteSource.Util.bytes(user.getCredentialsSalt()),//salt=username+salt
                getName()  //realm name
        );
        return authenticationInfo;
    }

    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(principals);
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    public void clearAllCachedAuthorizationInfo() {
        getAuthorizationCache().clear();
    }

    public void clearAllCachedAuthenticationInfo() {
        getAuthenticationCache().clear();
    }

    public void clearAllCache() {
        clearAllCachedAuthenticationInfo();
        clearAllCachedAuthorizationInfo();
    }

}
